// app.js
App({
  d: {
    hostUrl: 'https://wxplus.paoyeba.com/index.php',
    hostImg: 'http://img.ynjmzb.net',
    hostVideo: 'http://zhubaotong-file.oss-cn-beijing.aliyuncs.com',
    userId: 1,
    appId:"wxbffb33cdba403c38",
    appKey: "36a18e4e9cea7f2fb70878eb0cf37adb",
    ceshiUrl:'https://kzds.qianlh.com/index.php',
  },
  api:{
    url:"https://kzds.qianlh.com",
    kzdsurl:"https://kzds.qianlh.com/Api"
  },
  interface:{
    index_swper: "/FeijiUser/adlist",//首页轮播
    index_classification: "/FeijiUser/index",//首页分类
    pinji_list: "/FeijiUser/pinji_list",//拼机列表
    pinji_detail: "/FeijiUser/pinji_xq",//拼机详情
    buy_feiji_list: "/FeijiUser/buy_feiji_list",//在售飞机列表
    buy_feiji_xq: "/FeijiUser/buy_feiji_xq",//在售飞机列表
    pinji_create: "/FeijiUser/add_pingji",//发布拼机
    baojiji_list: "/FeijiUser/baoji_list",//包机列表
    baojiji_detail: "/FeijiUser/baoji_xq",//包机列表
    city_list: "/FeijiUser/address",//包机列表
  },
  globalDatainfo: {
    defaultCity: '',
    defaultCounty: '',
    weatherData: '',
    location:"",
    locationid:"",
    county:"",
    countyid:"",
    air: '',
    day: '',
    g_isPlayingMusic: false,
    g_currentMusicPostId: null,
    doubanBase: "https://api.douban.com",
    heWeatherBase: "https://free-api.heweather.com",
    juhetoutiaoBase: "https://v.juhe.cn/toutiao/index",
    tencentMapKey: "4HYBZ-EB23D-SLC42-HQ5R3-LP3LQ-OZFU5",
    heWeatherKey: "4a817b4338e04cc59bdb92da7771411e",
    juhetoutiaoKey: "a9f703a0200d68926f707f3f13629078",
    curBook: ""
  },
  globalData: {
    trainBeginCity: '杭州',
    trainEndCity: '北京',
    initplace:'',
    initcityid: '',
    targetcityid: '',
    targetplace:'',
    setid: '',
    oneWaystartCity: '请选择',
    oneWaycityid: '',
    oneWayendCity: '请选择',
    oneWayendcityid: '',
    returnstart: '请选择',
    returnscityid: '',
    returnend: '请选择',
    returnendcityid: '',
    morestart: '请选择',
    morescityid: '',
    moreend: '请选择',
    moreendcityid: '',
    choseaddress:{
      addrId: '',
      tel: '',
      name: '',
      addressed: ''
    },
    fpname: '',
    type: '',
    typeaddress: '',
    // 单程选择出发时间
    datedc:'',
    // 往返选择出发时间
    datewfs: '',
    // 往返选择返程时间
    datewfend: '',
    // 多程选择返程时间
    datemores: '',
    // 多程选择返程时间
    datemorend: '',
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    //login

    // 判断是否登录
    let userInfo = wx.getStorageSync('userInfo');
    let that=this;
    if (!userInfo) {
      that.goLoginPageTimeOut()
      return
    }

    //this.getUserInfo();
  },
  goLoginPageTimeOut: function () {
    setTimeout(function () {
      wx.navigateTo({
        url: "/pages/authorize/index"
      })
    }, 1000)
  },
  getUserInfo:function(cb){
     wx.login({
        success: function (res) {
          var code = res.code;
          //get wx user simple info
          wx.getUserInfo({
            success: function (res) {
          
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo);
              //get user sessionKey
              //get sessionKey
              that.getUserSessionKey(code);
            }
          });
        }
      });
  },

  getUserSessionKey:function(code){
    //用户的订单状态
    var that = this;
    wx.request({
      url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxbffb33cdba403c38&secret=36a18e4e9cea7f2fb70878eb0cf37adb&js_code=' + code + '&grant_type=authorization_code',
      data: {

      },
      success: function (res) {
        var openid = 
        that.globalData.userInfo['sessionId'] = res.data.session_key;
        that.globalData.userInfo['openid'] = res.data.openid;
        wx.setStorageSync('openid',res.data.openid);
        wx.setStorage({
          key: 'openid',
          data: res.data.openid
        })
       // debugger
        that.onLoginUser();
      }, 
      fail:function(e){
        wx.showToast({
          title: '网络异常！err:getsessionkeys',
          duration: 2000
        });
      }
    })

    // wx.request({
    //   url: that.d.ceshiUrl + '/Api/Login/getsessionkey',
    //   method:'post',
    //   data: {
    //     code: code
    //   },
    //   header: {
    //     'Content-Type':  'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //     //--init data
    //     var data = res.data;
    //     if(data.status==0){
    //       wx.showToast({
    //         title: data.err,
    //         duration: 2000
    //       });
    //       return false;
    //     }

    //     that.globalData.userInfo['sessionId'] = data.session_key;
    //     that.globalData.userInfo['openid'] = data.openid;
    //     that.onLoginUser();
    //   },
    //   fail:function(e){
    //     wx.showToast({
    //       title: '网络异常！err:getsessionkeys',
    //       duration: 2000
    //     });
    //   },
    // });
  },
  onLoginUser:function(){
    var that = this;
    var user = that.globalData.userInfo;
    wx.request({
      url: that.d.ceshiUrl + '/Api/Login/authlogin',
      method:'post',
      data: {
        SessionId: user.sessionId,
        gender:user.gender,
        NickName: user.nickName,
        HeadUrl: user.avatarUrl,
        openid:user.openid
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data
        var data = res.data.arr;
        var status = res.data.status;
        if(status!=1){
          wx.showToast({
            title: res.data.err,
            duration: 3000
          });
          return false;
        }
        that.globalData.userInfo['id'] = data.ID;
        that.globalData.userInfo['NickName'] = data.NickName;
        that.globalData.userInfo['HeadUrl'] = data.HeadUrl;
        var userId = data.ID;
        if (!userId){
          wx.showToast({
            title: '登录失败！',
            duration: 3000
          });
          return false;
        }
        that.d.userId = userId;
        wx.setStorageSync('userId', userId);
      },
      fail:function(e){
        wx.showToast({
          title: '网络异常！err:authlogin',
          duration: 2000
        });
      },
    });
  },
  getOrBindTelPhone:function(returnUrl){
    var user = this.globalData.userInfo;
    if(!user.tel){
      wx.navigateTo({
        url: 'pages/binding/binding'
      });
    }
  },

 globalData:{
    userInfo:null
  },

  onPullDownRefresh: function (){
    wx.stopPullDownRefresh();
  }

});





